﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Main_Master_ProductMaster : System.Web.UI.Page
{
    Datautility DUCON = new Datautility();
    string id = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetData();
        }
    }
    protected void GetData()
    {
        DataTable dt = DUCON.getdata("USP_ACC_Select_ProductMaster");
        if (dt.Rows.Count > 0)
        {
            grdproductmaster.DataSource = dt;
            grdproductmaster.DataBind();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            //DUCON.TransactionStart();
            if (btnSave.Text != "Update")
            {
                SqlParameter[] param = {new SqlParameter("@peccode",txtpeccode.Text.Trim()),
                                   new SqlParameter("@projectmodel",txtprojectmodel.Text.Trim()),
                                   new SqlParameter("@projectdesc",txtprojectdesc.Text.Trim())};
                int insert = DUCON.TranExecProcedure("USP_ACC_PRODUCT_MASTER", param);
                if (insert > 0)
                {
                    lblmsg.Text = "Product Inserted successfully.";
                    lblmsg.ForeColor = System.Drawing.Color.Green;
                    lblmsg.Attributes.Add("style", "display:block");                    
                    txtpeccode.Text = "";
                    txtprojectdesc.Text = "";
                    txtprojectmodel.Text = "";
                    GetData();
                }
            }
            else 
            {
                SqlParameter[] param = {new SqlParameter("@id",hidproduct.Value),
                                           new SqlParameter("@peccode",txtpeccode.Text.Trim()),
                                   new SqlParameter("@projectmodel",txtprojectmodel.Text.Trim()),
                                   new SqlParameter("@projectdesc",txtprojectdesc.Text.Trim())};
                int insert = DUCON.TranExecProcedure("ACC_Update_Product_Master", param);
                if (insert > 0)
                {
                    lblmsg.Text = "Product Updated successfully.";
                    lblmsg.ForeColor = System.Drawing.Color.Green;
                    lblmsg.Attributes.Add("style", "display:block");
                    //lblmsg.Style["display"] = "block";
                    txtpeccode.Text = "";
                    txtprojectdesc.Text = "";
                    txtprojectmodel.Text = "";
                    GetData();
                }
            }
            //DUCON.TransactionCommit();
        }
        catch (Exception ex)
        {
            //DUCON.TransactionRollBack();
            lblmsg.Text = "Error in Save Data.";
            lblmsg.ForeColor = System.Drawing.Color.Red;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtpeccode.Text = string.Empty;
        txtprojectdesc.Text = string.Empty;
        txtprojectmodel.Text = string.Empty;
        lblmsg.Attributes.Add("style", "display:none");
    }
    protected void lnkbtnEdit_Click(object sender, ImageClickEventArgs e)
    {
        //LinkButton lbtnid = (LinkButton)sender;
        ImageButton lbtnid = (ImageButton)sender;
        //id = lbtnid.CommandArgument;
        hidproduct.Value = lbtnid.CommandArgument;
        SqlParameter[] param = { new SqlParameter("@id",hidproduct.Value) };
        DataTable dt = DUCON.getdatausingprocedure("ACC_Select_ProductMasterByID", param);
        if (dt.Rows.Count > 0)
        {
            txtpeccode.Text = dt.Rows[0]["PecCode"].ToString();
            txtprojectdesc.Text = dt.Rows[0]["ProjectDesc"].ToString();
            txtprojectmodel.Text = dt.Rows[0]["ProjectModel"].ToString();
        }
        btnSave.Text = "Update";
    }
    protected void grdproductmaster_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void grdproductmaster_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdproductmaster.PageIndex = e.NewPageIndex;
        GetData();
    }
    
}